<template>
  <div class="app-container" ref="appcontainer">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
     <!-- <el-form-item label="openId" prop="openId">
        <el-input
          v-model="queryParams.openId"
          placeholder="请输入openId"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
   <!--   <el-form-item label="openId" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入openId"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
     <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="帐号状态" prop="state">-->
<!--        <el-select v-model="queryParams.state" placeholder="请选择状态是否正常" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in stateOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleConfigSet"
        >权益配置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleVipPrice"
        >会员价格</el-button>
      </el-col>
     <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:member:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain

          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:member:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain

          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:member:remove']"
        >删除</el-button>
      </el-col>-->
     <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain

          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:member:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :height="tableHeight" :data="memberList" @selection-change="handleSelectionChange">
    <!--  <el-table-column type="selection" width="55" align="center" />-->
      <!--<el-table-column label="主键" align="center" prop="id" />-->
<!--      <el-table-column label="openId" align="center" prop="openId" />-->
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="头像" align="center" prop="headImg">
        <template width="120" slot-scope="scope">
          <img style="width:60px;height:60px;border:none;" :src="scope.row.headImg">
        </template>
      </el-table-column>
      <el-table-column label="积分" align="center" prop="integral" />
      <el-table-column label="推荐人" align="center" prop="referrer" />
<!--      <el-table-column label="性别" align="center" prop="gender" />-->
  <!--    <el-table-column label="帐号状态是否正常" align="center" prop="state" >
        <template slot-scope="scope">
          <dict-tag :options="stateOptions" :value="scope.row.state"/>
        </template>
      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"

            @click="handleGiftCoupons(scope.row)"
            v-hasPermi="['system:member:edit']"
          >发放平台券</el-button>
         <!-- <el-button
            size="mini"
            type="text"

            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:member:edit']"
          >修改</el-button>-->
          <!--<el-button
            size="mini"
            type="text"

            @click="handleDelete(scope.row)"
            v-hasPermi="['system:member:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改客户信息对话框 -->
    <el-dialog title="优惠券发放" :visible.sync="couponsOpen" width="500px" append-to-body>
      <el-form ref="form" :model="selectedCoupons" label-width="80px">
        <el-form-item label="优惠券" prop="openId">
          <el-select v-model="selectedCoupons.id" placeholder="请选择优惠券" clearable size="small">
            <el-option
              v-for="coupon in couponsList"
              :key="coupon.id"
              :label="coupon.couponName"
              :value="coupon.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="gift">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改客户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="openId" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入openId" />
        </el-form-item>
        <el-form-item label="openId" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入openId" />
        </el-form-item>
        <el-form-item label="积分" prop="integral">
          <el-input v-model="form.integral" placeholder="请输入积分" />
        </el-form-item>
        <el-form-item label="推荐人" prop="referrer">
          <el-input v-model="form.referrer" placeholder="请输入推荐人" />
        </el-form-item>
        <el-form-item label="openId" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入openId" />
        </el-form-item>
        <el-form-item label="头像" prop="headImg">
          <el-input v-model="form.headImg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="帐号状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入帐号状态" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 配置修改表格弹窗 -->
    <el-dialog title="权益配置" :visible.sync="configVisible" width="800px" append-to-body>
      <el-table
        :data="configArry"
        style="width: 100%">
        <el-table-column
          prop="configName"
          label="参数名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="configValue"
          label="参数键值"
          width="180">
        </el-table-column>
        <el-table-column
          prop="remark"
          label="备注">
        </el-table-column>
        <el-table-column
      fixed="right"
      label="操作"
      width="100">
      <template slot-scope="scope">
        <el-button @click="handleConfigFix(scope.row)" type="text" size="small">修改</el-button>
      </template>
    </el-table-column>
      </el-table>
      <!-- <span slot="footer" class="dialog-footer">
        <el-button >取 消</el-button>
        <el-button type="primary" >确 定</el-button>
      </span> -->
    </el-dialog>
    <!-- 配置修改弹窗 -->
    <el-dialog title="权益配置修改" :visible.sync="configFixVsible" width="500px" append-to-body>
      <el-input v-model="configValueModel" ></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancleConfig">取 消</el-button>
        <el-button type="primary" @click="doUpdateConfig">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 会员价格修改 -->
    <el-dialog title="会员价格" :visible.sync="pricevisible" width="500px" append-to-body>
      <el-input type="number" v-model="configValue.configValue"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelVipPrice">取 消</el-button>
        <el-button type="primary" @click="doUpdateVipPrice">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listMember, getMember, delMember, addMember, updateMember, exportMember,canUseCoupons,getVipPrice,updateVipPrice,getMemberConfig,updateMemberConfig } from "@/api/member/member";
import { getCanGiftCoupons } from "@/api/coupons/coupons";

export default {
  name: "Member",
  data() {
    return {
      configVisible: false,//修改权益配置表格的弹窗
      configArry: [],//权益配置
      configFixVsible: false,//修改权益配置的弹窗
      configValueModel: '',//权益修改输入框绑定值
      curentRow: {}, //当前修改的权益配置对象数据
      configValue: {}, //会员价格
      pricevisible: false,
      tableHeight:0,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 客户信息表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //优惠券赠予弹窗
      couponsOpen:false,
      //优惠券列表
      couponsList:[],
      selectedCoupons:{},
      selectUserId:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        integral:null,
        referrer:null,
        openId: null,
        nickName: null,
        phone: null,
        headImg: null,
        gender: null,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 启用状态字典
      stateOptions: [],
    };
  },
  created() {
    this.getList();
    this.calHeight()
    this.getDicts("sys_yes_no").then(response => {
      this.stateOptions = response.data;
    });
        },
  watch: {
    'showSearch':'calHeight'
  },
  methods: {
    calHeight () {
          this.$nextTick(() => {
              const rect = this.$refs.appcontainer.getBoundingClientRect();
              let height= this.$refs.queryForm.$el.offsetHeight;
              this.tableHeight = rect.height-height-120;
          })
      },
    /** 查询客户信息列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 赠予优惠券 */
    handleGiftCoupons(row){
      getCanGiftCoupons().then(res=>{
        this.couponsList = res.rows
        this.couponsOpen = true
        this.selectUserId = row.id
      })
    },
    //确定发送优惠券
    gift(){
      if(!this.selectedCoupons.id){
        this.msgError("请选择优惠券");
        return
      }
      const params = {memberId:this.selectUserId,couponsId:this.selectedCoupons.id}
      canUseCoupons(params).then(res=>{
        this.couponsOpen = false
        this.msgSuccess("发放成功");
        this.selectedCoupons = {}
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.couponsOpen = false
      this.selectedCoupons = {}
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        openId: null,
        nickName: null,
        integral: null,
        referrer:null,
        phone: null,
        headImg: null,
        gender: null,
        state: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.showSearch=false;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMember(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除客户信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMember(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有客户信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportMember(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    // 会员价格
    handleVipPrice() {
      getVipPrice().then(res => {
        this.configValue = res.data.vipPrice
        this.pricevisible = true
      })
    },
    doUpdateVipPrice() {
      updateVipPrice(this.configValue).then((res) => {
        this.msgSuccess(res.msg)
        this.pricevisible = false
      })
    },
    cancelVipPrice() {
      this.pricevisible = false
    },
    //会员权益
    handleConfigSet() {
      getMemberConfig().then(res => {
        this.configArry = [
          {...res.data.memberOrderPercent},
          {...res.data.member_push}
        ]
        this.configVisible = true
      })
    },
    handleConfigFix(row) {
      //configValue
      this.curentRow = row
      this.configValueModel = row.configValue
      this.configFixVsible = true
    },
    //修改会员权益
    doUpdateConfig() {
      this.curentRow.configValue = this.configValueModel
      updateMemberConfig(this.curentRow).then(() => {
        this.msgSuccess('修改成功')
        this.configClose() //清空权益修改弹窗的输入值
        this.handleConfigSet() //重新获取权益配置
      })
    },
    //取消会员权益
    cancleConfig() {
      this.configClose()
    },
    configClose() {
      this.configValueModel = '',
      this.curentRow = {}
      this.configFixVsible = false
    }
  }
};
</script>
